在 Jenkins 从节点上运行的 Jenkinsfile 中执行 docker build 命令的最简单方法是什么?

您所在的位置:网站首页 jenkins docker build 在 Jenkins 从节点上运行的 Jenkinsfile 中执行 docker build 命令的最简单方法是什么?

在 Jenkins 从节点上运行的 Jenkinsfile 中执行 docker build 命令的最简单方法是什么?

2024-07-11 01:07:02| 来源: 网络整理| 查看: 265

本文介绍了在 Jenkins 从节点上运行的 Jenkinsfile 中执行 docker build 命令的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时送ChatGPT账号..

我希望 Jenkinsfile 执行的基本示例:

节点{sh 'docker build -t foo/bar .'}

看来我需要将 docker 安装到正在执行我的 Jenkinsfile 的 Jenkins 从属映像上.有没有一种简单的方法可以做到这一点?(那个 Jenkins 从镜像本身就是一个 docker 容器)

我的假设正确吗?

当与 Jenkins master/slave 一起运行时,Jenkinsfile 由 Jenkins slave 执行通过管理插件部分安装的 Jenkins 插件(例如 Docker 插件或 Gcloud SDK 插件)仅安装在 Jenkins 主机上,因此我需要手动构建我的 Jenkins 从属 docker 镜像并在镜像上安装 docker?

由于我还需要访问gcloud"命令(我通过 Kubernetes Helm/Charts 运行 Jenkins),我一直在使用 gcr.io/cloud-solutions-images/jenkins-k8s-我的 Jenkins 奴隶的奴隶图像.

目前错误提示docker: not found"

解决方案

我的假设是你想 docker build 在 Jenkins slave(这是一个 Kubernetes pod,我假设由

请注意,您使用不同的图像(您可以创建自己的图像并在其中添加任何您想要的二进制文件),并且您将 Docker 套接字安装在容器内.

第一种方法的问题在于,您创建了一个从官方 JNLP 从站派生的新镜像并手动添加 Docker 客户端.这意味着每当 Jenkins 或 Docker 有更新时,您都需要手动更新您的镜像和整个配置,这是不可取的.使用第二种方法,您始终使用官方镜像,并且您使用 JNLP 从属设备在同一个 pod 中启动其他容器.

正如我所说,JNLP 映像将启动您在同一个 pod 中指定的容器.请注意,为了从容器中使用 Docker,您仍然需要挂载 Docker sock.

这是我发现在容器内运行的 Jenkins JNLP slave 中构建映像的两种方法.

该示例还展示了如何使用来自 Jenkins 的凭据绑定推送映像,以及如何在构建过程中更新 Kubernetes 部署.

更多资源:

将 Jenkins 作为 Helm chart 部署到 Kubernetes,配置要安装的插件

谢谢,拉杜M

Basic example of what I want my Jenkinsfile to do:

node { sh 'docker build -t foo/bar .' }

It seems like I need to install docker onto the Jenkins slave image that's executing my Jenkinsfile. Is there an easy way of doing this? (That Jenkins slave image is itself a docker container)

Are my assumptions correct?

When running with Jenkins master/slaves, the Jenkinsfile is executed by a Jenkins slave Jenkins plugins installed via the Manage Plugins section (e.g. the Docker Plugin, or Gcloud SDK plugin) are only installed on the Jenkins masters, therefore I would need to manually build my Jenkins slave docker image and install docker on the image?

Since I also need access to the 'gcloud' command (I'm running Jenkins via Kubernetes Helm/Charts), I've been using the gcr.io/cloud-solutions-images/jenkins-k8s-slave image for my Jenkins slave.

Currently it errors out saying "docker: not found"

解决方案

My assumption is that you want to docker build inside the Jenkins slave (which is a Kubernetes pod, I assume created by the Kubernetes Jenkins Plugin)

To set the stage, when Kubernetes creates pod that will act as a Jenkins slave, all commands that you execute inside the node will be executed inside that Kubernetes pod, inside one of the containers there (by default there will only be one container, but more on this later).

So you are actually trying to run a Docker command inside a container based on gcr.io/cloud-solutions-images/jenkins-k8s-slave, which is most likely based on the official Jenkins JNLP Slave, which does not container Docker!

From this point forward, there are two approaches that you can take:

use a slightly modified image based on the JNLP slave that also contains the Docker client and mount the Docker socket (/var/run/docker.sock) inside the container. (You can find details on this approach here). Here is an image that contains the Docker client and kubectl.

Here is a complete view of how to configure the Jenkins Plugin:

Note that you use a different image (you can create your own and add any binary you want there) and that you mount the Docker socket inside the container.

the problem with the first approach is that you create a new image forked from the official JNLP slave and manually add the Docker client. This means that whenever Jenkins or Docker have updates, you need to manually update your image and entire configuration, which is not that desirable. Using the second approach you always use official images, and you use the JNLP slave to start other containers in the same pod.

Here is the full file from the image below

Here is the Jenkins Plugin documentation for doing this

As I said, the JNLP image will start a container that you specify in the same pod. Note that in order to use Docker from a container you still need to mount the Docker sock.

These are the two ways I found to achieve building images inside a Jenkins JNLP slave running inside a container.

The example also shows how to push the image using credential bindings from Jenkins, and how to update a Kubernetes deployment as part of the build process.

Some more resources:

deploy Jenkins to Kubernetes as Helm chart, configure plugins to install

Thanks, Radu M

这篇关于在 Jenkins 从节点上运行的 Jenkinsfile 中执行 docker build 命令的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭